﻿' --- Copyright (c) notice DnnConsulting.nl ---
'  Copyright (c) 2014 DnnConsulting.nl.  www.DnnConsulting.nl. BSD License.
' Author: G. M. Barlow
' ------------------------------------------------------------------------
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
' DEALINGS IN THE SOFTWARE.
' ------------------------------------------------------------------------
' This copyright notice may NOT be removed, obscured or modified without written consent from the author.
' --- End copyright notice --- 
' 
Imports DotNetNuke
Imports DotNetNuke.Entities.Modules.Actions
Imports DotNetNuke.Entities.Modules
Imports DotNetNuke.Services.Exceptions
Imports DotNetNuke.Services.Localization
Imports DotNetNuke.Common.Globals
Imports DotNetNuke.Web.Client.ClientResourceManagement

Imports Google.GData.Photos
Imports Google.GData.Client
Imports Google.GData.Extensions
Imports Google.GData.Extensions.Location

Public Class View
    Inherits DnnC_Picasa_ModuleBase
    'Implements IActionable

    Protected _picasaUsername As String
    Protected _picasaPassword As String
    Protected _defaultGalleryId As String = "-1"

#Region " Event Methods "

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        Try
            If Not Settings.Contains("PicasaUserName") Then pnlMessage.Visible = True
            If Not Settings.Contains("PicasaPassWord") Then pnlMessage.Visible = True

            If Not Page.IsPostBack Then
                If Settings.Contains("PicasaUserName") And Settings.Contains("PicasaPassWord") Then
                    _picasaUsername = Settings("PicasaUserName")
                    _picasaPassword = Settings("PicasaPassWord")

                    If Settings.Contains("PicasaDefaultGallery") Then
                        If Not Settings("PicasaDefaultGallery") = String.Empty Then
                            _defaultGalleryId = Settings("PicasaDefaultGallery")
                        End If
                    End If
                    SetUpStyle()
                    SetUpGallery()
                End If

            End If

        Catch ex As Exception
            ShowError(ex.ToString)
            Exceptions.ProcessModuleLoadException(Me, ex)
        End Try
    End Sub

#End Region

#Region "Pivate Methods"

    Private Sub SetUpStyle()
        Dim strStyle As StringBuilder = New StringBuilder
        strStyle.AppendLine("<style type=""text/css"">")
        If Settings.Contains("PicasaStyle") Then
            strStyle.AppendLine(Settings("PicasaStyle"))
        Else
            strStyle.AppendLine(".dnnc_picasaAlbums li {")
            strStyle.AppendLine("   float: left;")
            strStyle.AppendLine("   padding: 0;")
            strStyle.AppendLine("   margin: 3px;")
            strStyle.AppendLine("   list-style: none;")
            strStyle.AppendLine("   background:#fff;")
            strStyle.AppendLine("   height:220px;")
            strStyle.AppendLine("   width:170px;")
            strStyle.AppendLine("}")

            strStyle.AppendLine(".dnnc_picasaAlbums li img { }")

            strStyle.AppendLine(".dnnc_picasaAlbums .dnnc_galleryHolder { text-align:center; padding:5px; }")
            strStyle.AppendLine(".dnnc_picasaAlbums .dnnc_picasaThumb { height:150px; }")
            strStyle.AppendLine(".dnnc_picasaAlbums .dnnc_picasaTitle { height:40px; }")
            strStyle.AppendLine(".dnnc_picasaAlbums .dnnc_picasaCount { line-height:24px; }")
            strStyle.AppendLine(".dnnc_picasaImages li  { ")
            strStyle.AppendLine("   display:inline;")
            strStyle.AppendLine("   float: left;")
            strStyle.AppendLine("   padding: 0;")
            strStyle.AppendLine("   margin: 3px;")
            strStyle.AppendLine("   list-style: none;")
            strStyle.AppendLine("   background:#fff;")
            strStyle.AppendLine("   height:200px;")
            strStyle.AppendLine("   width:170px;")
            strStyle.AppendLine("}")
            strStyle.AppendLine(".dnnc_picasaImages li img { }")
            strStyle.AppendLine(".dnnc_picasaImages .dnnc_galleryHolder { text-align:center; padding:10px 5px 10px 5px; }")
            strStyle.AppendLine(".dnnc_picasaImages .dnnc_picasaImageThumb { height:150px; }")
            strStyle.AppendLine(".dnnc_picasaImages.dnnc_picasaImageTitle {}")
        End If

        strStyle.AppendLine("</style>")
        ltlStyle.Text = strStyle.ToString
    End Sub

    Private Sub SetUpGallery()
        If GalleryId = "-1" Then
            If _defaultGalleryId = "-1" Then
                GetPicasaAblums()
            Else
                GetPicasaPhotos(_defaultGalleryId)
                SetUpFancyBoxScripts()
            End If
        Else
            GetPicasaPhotos(GalleryId)
            SetUpFancyBoxScripts()
        End If
    End Sub

    Private Sub ShowError(errMessage As String)
        lblError.Text = errMessage
        pnlError.Visible = True
    End Sub

    Private Sub GetPicasaAblums()
        Try

            Dim strGroupTab As Integer = -1
            If Settings.Contains("PicasaGalleryPage") Then
                If CType(Settings("PicasaGalleryPage"), String) <> "" Then
                    strGroupTab = CInt(Settings("PicasaGalleryPage"))
                End If
            End If

            Dim pService As PicasaService = New PicasaService("dnnc.PicasaImageGallery")
            pService.setUserCredentials(_picasaUsername, _picasaPassword)  ' needed when you need to show albums with private visibility

            Dim qry As AlbumQuery = New AlbumQuery(PicasaQuery.CreatePicasaUri(_picasaUsername))
            If Settings.Contains("PicasaGalleryThumbSize") Then
                qry.Thumbsize = Settings("PicasaGalleryThumbSize") & "c"
            End If

            Dim feed As PicasaFeed = pService.Query(qry)

            Dim html As StringBuilder = New StringBuilder
            html.AppendLine("<div class=""dnnc_picasaAlbums"">")
            html.AppendLine("<ul>")

            For Each entry As PicasaEntry In feed.Entries
                Dim navUrl As String = String.Empty

                If strGroupTab = -1 Then
                    navUrl = NavigateURL(Me.TabId, "", "gid=" & entry.GetPhotoExtensionValue(Google.GData.Photos.GPhotoNameTable.Id))
                Else
                    navUrl = NavigateURL(strGroupTab, "", "gid=" & entry.GetPhotoExtensionValue(Google.GData.Photos.GPhotoNameTable.Id))
                End If

                html.AppendLine("<li>")
                html.AppendLine("   <div class=""dnnc_galleryHolder"">")

                '// BEGIN
                If Settings.Contains("PicasaGalleryTitlePosition") Then

                    If Settings("PicasaGalleryTitlePosition") = "top" Then
                        html.AppendLine("       <div class=""dnnc_picasaTitle""><a href=""" & navUrl & """ class=""Title"">" & entry.Title.Text & "</a></div>")
                        html.AppendLine("       <div class=""dnnc_picasaThumb""><a href=""" & navUrl & """><img src=""" & entry.Media.Thumbnails(0).Url & """ /></a></div>")
                    End If

                    If Settings("PicasaGalleryTitlePosition") = "bottom" Then
                        html.AppendLine("       <div class=""dnnc_picasaThumb""><a href=""" & navUrl & """><img src=""" & entry.Media.Thumbnails(0).Url & """ /></a></div>")
                        html.AppendLine("       <div class=""dnnc_picasaTitle""><a href=""" & navUrl & """ class=""Title"">" & entry.Title.Text & "</a></div>")
                    End If

                    If Settings("PicasaGalleryTitlePosition") = String.Empty Then
                        html.AppendLine("       <div class=""dnnc_picasaThumb""><a href=""" & navUrl & """><img src=""" & entry.Media.Thumbnails(0).Url & """ /></a></div>")
                    End If

                Else
                    html.AppendLine("       <div class=""dnnc_picasaThumb""><a href=""" & navUrl & """><img src=""" & entry.Media.Thumbnails(0).Url & """ /></a></div>")
                End If
                '// End 

                If Settings.Contains("PicasaDisplayImageCount") Then
                    If Settings("PicasaDisplayImageCount") Then
                        html.AppendLine("       <div class=""dnnc_picasaCount"">" & _
                            [String].Format("<span class=""DnnC_CatTextStyle "">{0} " & _
                            Localization.GetString("lblImage.Text", Me.LocalResourceFile) & _
                            "</span>", entry.GetPhotoExtensionValue(Google.GData.Photos.GPhotoNameTable.NumPhotos)) & _
                            "</div>")
                    End If
                End If

                html.AppendLine("   </div")
                html.AppendLine("</li>")
            Next

            html.AppendLine("</ul>")
            html.AppendLine("</div>")
            ltlGallery.Text = html.ToString()

        Catch ex As Exception
            ShowError(ex.ToString)
        End Try
    End Sub

    Private Sub GetPicasaPhotos(AlbumId As String)
        Try
            If _defaultGalleryId = "-1" Then
                btnBack1.Visible = True
                btnBack2.Visible = True
                btnBack1.Text = Localization.GetString("btnBack.Text", LocalResourceFile)
                btnBack2.Text = Localization.GetString("btnBack.Text", LocalResourceFile)

                btnBack1.NavigateUrl = NavigateURL(Me.TabId, "")
                btnBack2.NavigateUrl = NavigateURL(Me.TabId, "")
            End If

            Dim pService As PicasaService = New PicasaService("dnnc.PicasaImageGallery")
            pService.setUserCredentials(_picasaUsername, _picasaPassword)  ' needed when you need to show albums with private visibility

            Dim qry As PhotoQuery = New PhotoQuery(PicasaQuery.CreatePicasaUri(_picasaUsername, AlbumId) & "?thumbsize=" & CType(Settings("PicasaImageThumbSize"), String) & "u")
            Dim feed As PicasaFeed = pService.Query(qry)

            Dim html As StringBuilder = New StringBuilder
            html.AppendLine("<div class=""dnnc_picasaImages"">")
            html.AppendLine("<ul>")

            For Each Entry As PicasaEntry In feed.Entries
                Dim title As String = Entry.Title.Text.Substring(0, Entry.Title.Text.LastIndexOf("."))




                html.AppendLine("<li>")
                html.AppendLine("   <div class=""dnnc_galleryHolder"">")


                If Settings.Contains("PicasaImageTitlePosition") Then
                    Dim iTitle As String = StripExtensionFromFileName(Entry.Title.Text)

                    If Settings("PicasaImageTitlePosition") = "top" Then
                        html.AppendLine("   <div class=""dnnc_picasaImageTitle"">" & iTitle & "</div>")
                        html.AppendLine("   <a class=""fancybox"" rel=""Gallery"" href=""" & Entry.Media.Content.Url & """ >")
                        html.AppendLine("       <div class=""dnnc_picasaImageThumb""><img src=""" & Entry.Media.Thumbnails(0).Url & """ alt=""" & Entry.Summary.Text & """/></div>")
                        html.AppendLine("   </a>")
                    End If

                    If Settings("PicasaImageTitlePosition") = "bottom" Then
                        html.AppendLine("   <a class=""fancybox"" rel=""Gallery"" href=""" & Entry.Media.Content.Url & """ >")
                        html.AppendLine("       <div class=""dnnc_picasaImageThumb""><img src=""" & Entry.Media.Thumbnails(0).Url & """ alt=""" & Entry.Summary.Text & """/></div>")
                        html.AppendLine("   </a>")
                        html.AppendLine("   <div class=""dnnc_picasaImageTitle"">" & iTitle & "</div>")
                    End If

                    If Settings("PicasaImageTitlePosition") = String.Empty Then
                        html.AppendLine("   <a class=""fancybox"" rel=""Gallery"" href=""" & Entry.Media.Content.Url & """ >")
                        html.AppendLine("       <div class=""dnnc_picasaImageThumb""><img src=""" & Entry.Media.Thumbnails(0).Url & """ alt=""" & Entry.Summary.Text & """/></div>")
                        html.AppendLine("   </a>")
                    End If

                Else

                    html.AppendLine("   <a class=""fancybox"" rel=""Gallery"" href=""" & Entry.Media.Content.Url & """ >")
                    html.AppendLine("       <div class=""dnnc_picasaImageThumb""><img src=""" & Entry.Media.Thumbnails(0).Url & """ alt=""" & Entry.Summary.Text & """/></div>")
                    html.AppendLine("   </a>")

                End If







                html.AppendLine("   </div")
                html.AppendLine("</li>")
            Next

            html.AppendLine("</ul>")
            html.AppendLine("</div>")

            ltlGallery.Text = html.ToString()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub SetUpFancyBoxScripts()
        Dim strUrl As String = "~/DesktopModules/DnnC_Picasa_Gallery/js/fancybox/"

        ClientResourceManager.RegisterScript(Me.Page, Me.Page.ResolveUrl(strUrl & "lib/jquery.mousewheel-3.0.6.pack.js"), 1)
        ClientResourceManager.RegisterStyleSheet(Me.Page, Me.Page.ResolveUrl(strUrl & "source/jquery.fancybox.css?v=2.1.5"), 1)
        ClientResourceManager.RegisterScript(Me.Page, Me.Page.ResolveUrl(strUrl & "source/jquery.fancybox.pack.js?v=2.1.5"), 2)

        Dim strScript As StringBuilder = New StringBuilder
        strScript.AppendLine("<script>")
        strScript.AppendLine("  $(document).ready(function() {")
        strScript.AppendLine("    $("".fancybox"").fancybox({")
        strScript.AppendLine("        openEffect:  'none',")
        strScript.AppendLine("        closeEffect:  'none'")
        strScript.AppendLine("    });")
        strScript.AppendLine("  });")
        strScript.AppendLine("</script>")
        ltlScript.Text = strScript.ToString
    End Sub

    Private Function StripExtensionFromFileName(strTitle As String) As String
        Dim retVal As String = String.Empty
        retVal = strTitle.Substring(0, strTitle.Length - 4)
        Return retVal
    End Function

#End Region

End Class